<template>
  <div>
    <van-submit-bar :price="getSum" button-text="提交订单"  @submit="onSubmit">
      <van-checkbox v-model="allchecked">全选</van-checkbox>
      <van-button
        type="primary"
        size="mini"
        color="linear-gradient(to right, #ff6034, #ee0a24)"
        v-if="allchecked"
        @click="removeall"
        >清空</van-button
      >
    </van-submit-bar>
    
  </div>
</template>

<script>
import Vue from "vue";
import { SubmitBar, Button, Dialog, Toast } from "vant";

import { selectall, removeall} from "../../apis/cart";
import { submitOrder} from "../../apis/order";

Vue.use(SubmitBar);
Vue.use(Button);
Vue.use(Dialog);
Vue.use(Toast);
export default {
  name: "Submit",
  props: ["list"],
  data() {
    return {};
  },

  mounted() {},

  methods: {
    removeall() {
      Dialog.confirm({
        message: "亲，确认清空购物车么",
      })
        .then(() => {
          // 清除购物车
          removeall({ userid: localStorage.getItem("userid") }).then((data) => {
            Toast.success(data.data);
            this.$router.go(0)
          });
        })
        .catch(() => {
          // on cancel
        });
    },
    // 提交订单
   async onSubmit(){
     await submitOrder({userid:localStorage.getItem("userid")}).then(data=>
       localStorage.setItem("ordertime",data.data.time) 
      )
      this.$router.push("/order")
    }
  },
  computed: {
    getSum() {
      let newArr = this.list.filter((item) => item.flag);
      return newArr.reduce(
        (sum, item) => (sum += item.originprice * item.num * 100),
        0
      );
    },

    allchecked: {
      get() {
        return this.list.every((item) => item.flag);
      },
      set(val) {
        this.list.forEach((item) => (item.flag = val));
        selectall({ userid: localStorage.getItem("userid"), type: val }).then();
      },
    },
  },
};
</script>

<style scoped>
.van-submit-bar {
  bottom: 0.65rem;
  opacity: 0.95;
}
</style>